package com.yhb.controller;

import com.yhb.channel.ChannelDispatcher;
import com.yhb.channel.ChannelFactory;
import com.yhb.channel.impl.ANYPAY.AnyPayChannel;
import lombok.extern.log4j.Log4j;
import me.edzh.spring.bootstrap.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.MultivaluedMap;
import java.util.HashMap;

@Controller
@Log4j
@RequestMapping("/channel/callback")
public class PayChannelCallbackController extends BaseController {
    @Autowired
    private ChannelDispatcher channelDispatcher;

    @Autowired
    private AnyPayChannel anyPayChannel;

    @PostMapping("/{channelType}/{action}/{orderCreateTimestamp}")
    public void channelCallback (
            @PathVariable("channelType") String channelType,
            @PathVariable("action") String callBackAction,
            @PathVariable("orderCreateTimestamp") String orderCreateTimestamp, // 订单创建时间戳(ms)
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value = "info", required = false, defaultValue = "") String info // querystring里放额外信息
    ) {
        log.info(channelType + "收到回调，类型：" + callBackAction
                + " \n orderCreateTimestamp: " + orderCreateTimestamp
                + " \n info: " + info);
        channelDispatcher.getPayChannelInfoByType(channelType).getPayChannel().handleCallback(
                callBackAction, request, response, orderCreateTimestamp, info);
    }
}
